<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.view');

class ScorecardViewScore extends JView {

	function display($tmpl = null) {
		global $option, $mainframe;

		$model =& $this->getModel();
		$country_list = $model->getCountries();
		$countries = array();
		$countries[] = array('value' => '0', 'text' => 'All countries');

		if (count($country_list) > 0) {
			for ($i = 0, $c = count($country_list); $i < $c; $i++) {
				$countries[] = array('value' => $country_list[$i]->id, 'text' => $country_list[$i]->name);
			}
		}
		$country_select = JHTML::_('select.genericlist', $countries, 'countryid', 'class="inputbox" onchange="this.form.submit()"' . '', 'value', 'text', $model->getCountryId());
		$this->assignRef('country', $country_select);


		$state_list = $model->getStates();
		$states = array();
		$states[] = array('value' => '0', 'text' => 'All states');
		if (count($state_list) > 0) {
			for ($i = 0, $c = count($state_list); $i < $c; $i++) {
				$states[] = array('value' => $state_list[$i]->id, 'text' => $state_list[$i]->name);
			}
		}
		$state_select = JHTML::_('select.genericlist', $states, 'stateid', 'class="inputbox" onchange="this.form.submit()"' . ' ', 'value', 'text', $model->getStateId());
		$this->assignRef('state', $state_select);

		$course_list = $model->getCourses();
		$courses = array();
		$courses[] = array('value' => '0', 'text' => 'All courses');
		if (count($course_list) > 0) {
			for ($i = 0, $c = count($course_list); $i < $c; $i++) {
				$courses[] = array('value' => $course_list[$i]->id, 'text' => $course_list[$i]->name);
			}
		}
		$course_select = JHTML::_('select.genericlist', $courses, 'courseid', 'class="inputbox"' . ' ', 'value', 'text', $model->getCourseId());
		$this->assignRef('course', $course_select);

		$task = JRequest::getVar('task');
		if ($task == 'scoreSearch') {
			// Adding pagination support
			require_once(JPATH_COMPONENT . DS . 'utils' . DS . 'scorecard.pager.php');
			$page = JRequest::getVar('pagerpage', 1);
			if (!$page) $page = 1;

			$pagesize = JRequest::getVar('limit', 10);
			$startindex = $page ? ($page - 1) * $pagesize : 0;

			$scores = $model->searchScores($startindex, $pagesize);

			$this->assignRef('scores', $scores['rows']);
			$this->assignRef('handicap', $scores['handicap']);

			$pager = new ScorecardPager(array('total' => $scores['total'],
											  'page' => $page,
											  'pagesize' => $pagesize,
											  'action' => 'scoreSearch',
											  'formname' => 'scoreform'));
			if ($pager->pagecount > 1) {
				$this->assignRef('pagination', $pager);
			}
		}

		$todate = JRequest::getVar('todate', '', '', 'string');
		// Menu item id
		$itemid = JRequest::getVar('Itemid', '', '', 'int');
		$this->assign('fromdate', $model->getFromDate());
		$this->assign('todate', $model->getToDate());
		$this->assignRef('itemid', $itemid);

		parent::display($tmpl);
	}
}
?>
